<?php namespace Home\Controller; 
use Think\Controller;

/**
 * Class 问题页控制器
 * @package Home\Controller
 */
class ContgController extends CommonController
{
	public function index()
	{
		$sid=$_SESSION['id'];
		//接收GET问题ID 
		$id=$_GET['id'];
		//浏览量
		if ($id) {
			$sql="update question set view_num=view_num+1 where id='{$id}'";
			$res=M('question')->execute($sql);
			// var_dump($res);
		}
		$sql="select * from answer where qid='{$id} limit 1'";
		$ifanswer=M('answer')->query($sql);
		$this->assign('ifanswer',$ifanswer);

		//查询发布问题的信息  多表查询
		//问题
		$sql="select t1.*, t2.nickname, t2.face from question t1 inner join user t2 on t1.uid=t2.id where t1.id='{$id}'";
		$res=M()->query($sql);
		$res=current($res);
		$this->assign('info',$res);
		$this->assign('title',$res['title']);
		//收藏
		$sql="select * from store where qid=$id and uid='{$sid}'";
		$res=M('store')->query($sql);
		$this->assign('store',$res);

		//回复 --  获取点赞ID
		$sql="select t1.*,t2.nickname, t2.face,(select id from zan where aid=t1.id and fuid='{$sid}') as fuid from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid='{$id}' order by t1.status desc" ;
		$res=M()->query($sql);
		$this->assign('asinfo',$res);

		$sql="select title,id,view_num from question order by view_num desc limit 10";
		$res=M('user')->query($sql);
		$this->assign('hotview',$res);

		$sql="select title,id,answer_num from question order by view_num desc limit 10";
		$res=M('user')->query($sql);
		$this->assign('hotanswer',$res);

		$this->display();
	}

	public function asadd()
	{
		//获取POST表单信息
		$ansid=$_POST['answerid'];
		if ($ansid) {
			$sql="select nickname from answer t1 inner join user t2 on t1.uid=t2.id where t1.id={$ansid}";
			$res=M('user')->query($sql);
			$res=current($res);
			$nickname='@'.$res['nickname'];
			$res=stristr($_POST['content'],$nickname);
			if ($res) { // 条件成立 回复层主
				$data=$_POST;
				$data['fid']=$ansid; //增加父ID
				$data['create_time']=time(); //增加时间
				$data['updata']=time();
				$data['uid']=$_SESSION['id']; 	//增加用户 谁回复的 
				$data['content']=htmlspecialchars($data['content']); //转实体  存数据
				$id=$data['qid']; 

				$this->autonum('question','answer_num','+1',$id); // 增加问题回答量

				//增加回复
				if (M('answer')->add($data)) {
					//增加消息--楼主
					$qid=$_POST['qid'];
					$sql="select uid from question where id='{$qid}'";
					$res=current(M('question')->query($sql));
					$data['dst_user_id']=$res['uid'];
					$data['dst_qid']=$_POST['qid'];
					$data['event_id']=$_SESSION['id'];
					$data['status']='1';
					$data['create_time']=time();
					M('msg')->add($data);
					//增加消息--层主
					$sql="select uid from answer where id='{$ansid}'";
					$res=current(M('answer')->query($sql));
					$data['dst_user_id']=$res['uid'];
					$data['dst_qid']=$_POST['qid'];
					$data['event_id']=$_SESSION['id'];
					$data['status']='2';
					$data['create_time']=time();
					M('msg')->add($data);

					$arr = array(
		    			'error'=>0,
		    			'info'=>'回复成功，点击“确定”刷新页面！'
		    			);
		    		echo json_encode($arr);
		    		exit;
				}else{
					$arr = array(
		    			'error'=>1,
		    			'info'=>'回答失败'
		    			);
		    		echo json_encode($arr);
		    		exit;
				}
			}else{ // 条件不成立直接回复楼主
				$data=$_POST;
				$data['create_time']=time();
				$data['updata']=time();
				$data['uid']=$_SESSION['id'];
				$data['content']=htmlspecialchars($data['content']);
				$id=$data['qid'];

				$this->autonum('question','answer_num','+1',$id);

				//增加回复
				if (M('answer')->add($data)) {
					//增加消息
					$qid=$_POST['qid'];
					$sql="select uid from question where id='{$qid}'";
					$res=current(M('question')->query($sql));

					$data['dst_user_id']=$res['uid'];
					$data['dst_qid']=$_POST['qid'];
					$data['event_id']=$_SESSION['id'];
					$data['status']='1';
					$data['create_time']=time();
					M('msg')->add($data);

					$arr = array(
		    			'error'=>0,
		    			'info'=>'回答成功!'
		    			);
		    		echo json_encode($arr);
		    		exit;
				}else{
					$arr = array(
		    			'error'=>1,
		    			'info'=>'回答失败'
		    			);
		    		echo json_encode($arr);
		    		exit;
				}
			}
			
		}else{	// 条件不成立直接回复楼主
			$data=$_POST;
			$data['create_time']=time();
			$data['updata']=time();
			$data['uid']=$_SESSION['id'];
			$data['content']=htmlspecialchars($data['content']);
			$id=$data['qid'];
			$this->autonum('question','answer_num','+1',$id);
			//增加回复
			if (M('answer')->add($data)) {
				//增加消息
				$qid=$_POST['qid'];
				$sql="select uid from question where id='{$qid}'";
				$res=current(M('question')->query($sql));

				$data['dst_user_id']=$res['uid'];
				$data['dst_qid']=$_POST['qid'];
				$data['event_id']=$_SESSION['id'];
				$data['status']='1';
				$data['create_time']=time();
				M('msg')->add($data);


				$arr = array(
	    			'error'=>0,
	    			'info'=>'回答成功!'
	    			);
	    		echo json_encode($arr);
	    		exit;
			}else{
				$arr = array(
	    			'error'=>1,
	    			'info'=>'回答失败'
	    			);
	    		echo json_encode($arr);
	    		exit;
				}
		}
		exit();
	}


	//点赞
	public function Zan_Answer()
	{
		$id=$_SESSION['id'];
		$answerid=$_POST['data'];
		$sql="select t2.id from answer t1 inner join question t2 on t1.qid=t2.id where t1.id='{$answerid}'"; 
		$qid=M()->query($sql);
		$qid=current($qid);
		if (!empty($id)) {
			$sql="select * from zan where aid='{$answerid}' and fuid='{$id}'";
			$res=M()->query($sql);
			$res=current($res);
			$zanid=$res['id'];
			$time=time();
			if (!empty($res)) {
				M('zan')->where("id='{$zanid}'")->delete();
				$this->autonum('answer','zan_num','-1',$answerid);
				$arr=array(
				'error'=>2,
				'info'=>'赞 - 1'
				);
				echo json_encode($arr);
				exit();

			}else{
				$data['qid']=$qid['id'];
				$data['aid']=$answerid;
				$data['fuid']=$id;
				$data['state']=1;
				$data['create_time']=time();
				M('zan')->add($data);

				//增加消息--层主
				$sql="select uid from answer where id='{$answerid}'";
				$res=current(M('answer')->query($sql));
				$data['dst_user_id']=$res['uid'];
				$data['dst_qid']=$qid['id'];
				$data['event_id']=$_SESSION['id'];
				$data['status']='3';
				$data['create_time']=time();
				M('msg')->add($data);
				//增加点赞量
				$this->autonum('answer','zan_num','+1',$answerid);
				$arr=array(
				'error'=>1,
				'info'=>'赞 + 1'
				);
				echo json_encode($arr);
				exit();
			}
		}else{
			$arr=array(
				'error'=>0,
				'info'=>'您还未登录，请先登录!'
				);
			echo json_encode($arr);
			exit();
		}
	}


	//收藏
	public function Store()
	{

		$qid=$_POST['data'];
		$uid=$_SESSION['id'];
		$sql="select * from store where uid='{$uid}' and qid='{$qid}'";
		$res=M('store')->query($sql);
		$res=current($res);
		$id=$res['id'];
		$time=time();
		if (!empty($uid)) {
			if (!empty($res)) { //状态不为空--删除收藏
				//删除收藏
				M('store')->where("id='{$id}'")->delete();
				//删除收藏量
				$this->autonum('question','store_num','-1',$qid);
				$arr=array(
				'error'=>2,
				'info'=>'取消收藏'
				);
				echo json_encode($arr);
				exit();
			}else{
				//增加收藏
				$data['qid']=$qid;
				$data['uid']=$uid;
				$data['state']=1;
				$data['create_time']=time();
				M('store')->add($data);

				//收藏消息---楼主
				$sql="select uid from question where id='{$qid}'";
				$res=current(M('question')->query($sql));
				$data['dst_user_id']=$res['uid'];
				$data['dst_qid']=$qid;
				$data['event_id']=$_SESSION['id'];
				$data['status']='4';
				$data['create_time']=time();
				M('msg')->add($data);

				//增加收藏量
				$this->autonum('question','store_num','+1',$qid);
				$arr=array(
				'error'=>1,
				'info'=>'收藏成功'
				);
				echo json_encode($arr);
				exit();
			}
		}else{
			$arr=array(
				'error'=>0,
				'info'=>'您还未登录，请先登录!'
				);
			echo json_encode($arr);
			exit();
		}
	}
	//采纳
	public function adopt()
	{

		$aid=$_POST['aid'];  //接收回答ID 
		//  通过问题ID   得到问题ID 问题发布人ID
		$sql="select qid,uid from answer where id='{$aid}'";
		$res=M('answer')->query($sql);
		$res=current($res); 
		$qid=$res['qid'];
		$uid=$res['uid'];
		
		$sql="update answer set status='1' where id='{$aid}'"; // 采纳一条回答 
		if(M('answer')->execute($sql)){ 
			//采纳成功 更新问题状态
			$sql="update question set status='1' where id='{$qid}'";
			M('question')->execute($sql);

			//通过问题ID 得到悬赏分
			$sql="select id,kiss_num from question where id='{$qid}'";
			$res=M('question')->query($sql);
			$question=current($res);
			$kiss_num=$question['kiss_num'];

			//悬赏分加给采纳人
			$sql="update user set kiss=kiss+{$kiss_num} where id='{$uid}'";
			$res=M('user')->execute($sql);

			//增加采纳消息--层主
			$data['dst_user_id']=$uid;
			$data['dst_qid']=$qid;
			$data['event_id']=$_SESSION['id'];
			$data['status']='5';
			$data['create_time']=time();
			M('msg')->add($data);

			$arr=array(
				'error'=>1,
				'info'=>'采纳成功，点击确定刷新页面！'
				);
			echo json_encode($arr);
			exit();
		}else{
			$arr=array(
				'error'=>0,
				'info'=>'采纳失败，请重试！'
				);
			echo json_encode($arr);
			exit();
		}

	}





}
?>